package com.nanxun.service.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanxun.constant.BusinessEnum;
import com.nanxun.domain.Category;
import com.nanxun.domain.Prod;
import com.nanxun.domain.ProdTagReference;
import com.nanxun.ex.handler.BusinessException;
import com.nanxun.feign.SearchProdFeign;
import com.nanxun.model.Result;
import com.nanxun.service.SearchService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SearchServiceImpl implements SearchService {

    @Resource
    private SearchProdFeign searchProdFeign;
    @Autowired
    private SearchService searchService;

    @Override
    public Page<Prod> queryWxProdPageByTagId(Long current, Long size, Long tagId) {
        //商品和分组标签他们之间是多对多的关系，现在要根据分组标签id分页去查商品，只能先分页查他们的关系，然后再根据关系查商品
        //创建商品分页对象
        Page<Prod> prodPage = new Page<>(current,size);
        //远程接口调用：根据分组标签分页查询商品与分组标签的关系
        Result<Page<ProdTagReference>> result = searchProdFeign.getProdTagReferencePageByTagId(current, size, tagId);
        //判断是否操作成功
        if (BusinessEnum.OPERATION_FAIL.getCode().equals(result.getCode())){
            throw new BusinessException("远程接口调用：根据分组标签分页查询商品与分组标签的关系失败");
        }
        //获取商品与分组标签的分页对象
        Page<ProdTagReference> prodTagReferencePage = result.getData();
        //从商品与分组标签分页对象中获取商品与分组标签关系记录
        List<ProdTagReference> prodTagReferenceList = prodTagReferencePage.getRecords();
        //判断商品与分组标签关系集合是否有值
        if(CollectionUtils.isEmpty(prodTagReferenceList) || prodTagReferenceList.size() == 0){
            //说明没有数据
            return prodPage;
        }
        //从商品与分组标签关系集合中获取商品id集合
        List<Long> prodIdList = prodTagReferenceList.stream().map(ProdTagReference::getProdId).collect(Collectors.toList());
        //远程调用：根据商品id集合查询商品对象集合
        Result<List<Prod>> prodResult = searchProdFeign.getProdListByIds(prodIdList);
        //判断是否操作成功
        if (prodResult.getCode().equals(BusinessEnum.OPERATION_FAIL.getCode())){
                throw new BusinessException("远程调用：根据商品id集合查询商品对象集合失败");
        }
        //获取商品对象集合
        List<Prod> prods = prodResult.getData();
        //组装商品分页对象
        prodPage.setRecords(prods);
        prodPage.setTotal(prodTagReferencePage.getTotal());
        prodPage.setPages(prodTagReferencePage.getPages());
        return prodPage;
    }


    /**
     *
     * 根据商品类目标识查询商品集合
     *  1.当前类目标识只有商品一级类目
     *  2.查询的商品应该包含商品一级类目下的子类目的商品
     * @param categoryId
     * @return java.util.List<com.nanxun.domain.Prod>
     * @author nanxun
     * @create 2025/6/24
     **/

    @Override
    public List<Prod> queryWxProdListByCategoryId(Long categoryId) {
        //创建所有类目id集合
        List<Long> allCategoryIds=new ArrayList<>();
        allCategoryIds.add(categoryId);
        //远程调用：根据商品一级类目id查询商品子类目集合
        Result<List<Category>> categoryResult = searchProdFeign.getCategoryListByParentId(categoryId);
        //判断是否操作成功
        if(categoryResult.getCode().equals(BusinessEnum.OPERATION_FAIL.getCode())){
            throw new BusinessException("远程调用：根据商品一级类目id查询商品子类目集合失败");
        }
        //获取数据
        List<Category> categoryList = categoryResult.getData();
        //判断商品子类目集合是否有值
        if (CollectionUtil.isNotEmpty(categoryList) && categoryList.size() !=0){
            //从子类目集合中获取类目id集合
            List<Long> categoryIdList = categoryList.stream().map(Category::getCategoryId).collect(Collectors.toList());
            allCategoryIds.addAll(categoryIdList);
        }
        //远程调用：根据商品类目id集合查询商品对象集合
        Result<List<Prod>> prodResult = searchProdFeign.getProdListByCategoryIds(allCategoryIds);
        //判断查询结果
        if (prodResult.getCode().equals(BusinessEnum.OPERATION_FAIL.getCode())){
            throw new BusinessException("远程调用失败：根据商品类目id集合查询商品对象集合");
        }
        //获取数据
        List<Prod> prods = prodResult.getData();
        return prods;
    }
}
